//
//  CPSHTasksViewController.m
//  CardMoveGame
//
//  Created by peac on 2025/11/10.
//

#import "CPSHTasksViewController.h"

@interface CPSHTasksViewController ()
@property(nonatomic,strong)NSArray *mdtaskArr;
@end

@implementation CPSHTasksViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImageView *drawbgIv=[[UIImageView alloc]initWithFrame:self.view.bounds];
    drawbgIv.contentMode = UIViewContentModeScaleAspectFill;
    drawbgIv.image=[UIImage imageNamed:@"bjtask"];
    [self.view addSubview:drawbgIv];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-self.view.frame.size.height*0.072*3.1,  self.view.frame.size.height*0.2, self.view.frame.size.height*0.072*6.2, self.view.frame.size.height*0.7) style:UITableViewStylePlain];
    tableView.backgroundColor=[UIColor clearColor];
    tableView.backgroundView=nil;
    tableView.opaque = NO;
    
    // 移除分隔线
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = self.view.frame.size.height*0.092;
    [self.view addSubview:tableView];
    self.mdtaskArr=@[@2,@4,@3,@10,@50,@300];
    
    
    

    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"TaskCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        
        // 设置单元格透明
        cell.backgroundColor = [UIColor clearColor];
        cell.contentView.backgroundColor = [UIColor clearColor];
        cell.backgroundView = nil;
        cell.opaque = NO;
        cell.selectionStyle = UITableViewCellSelectionStyleNone; // 取消选中效果
    }
    
    // 移除之前的背景图片（避免重用时的重叠）
    for (UIView *subview in cell.contentView.subviews) {
        if ([subview isKindOfClass:[UIImageView class]]) {
            [subview removeFromSuperview];
        }
    }
    cell.contentView.backgroundColor=[UIColor clearColor];
    UIImageView *bgimage=[[UIImageView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height*0.01,tableView.frame.size.width, self.view.frame.size.height*0.072)];
    bgimage.image=[UIImage imageNamed:@"任务"];
    [cell.contentView addSubview:bgimage];
    
    UILabel *countLab=[[UILabel alloc]initWithFrame:CGRectMake(20, cell.contentView.frame.size.height*0.77, tableView.frame.size.width/2,cell.contentView.frame.size.height/2)];
    countLab.textColor=[UIColor blackColor];
    [cell.contentView addSubview:countLab];
    UIButton *claimBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    claimBtn.frame=CGRectMake(tableView.frame.size.width-self.view.frame.size.height*0.036*2.53-30, self.view.frame.size.height*0.015, self.view.frame.size.height*0.036*2.53, self.view.frame.size.height*0.036);
    [cell.contentView addSubview:claimBtn];
    claimBtn.tag=indexPath.row;
    NSUserDefaults *defaul=[NSUserDefaults standardUserDefaults];
    if (indexPath.row<2) {
        countLab.text=[NSString stringWithFormat:@"%ld/%d",(long)[defaul integerForKey:@"mdshop"],[self.mdtaskArr[indexPath.row] intValue]];
//        if ([defaul integerForKey:@"mdshop"]<[self.mdtaskArr[indexPath.row] intValue]) {
//            
//        }else{
            if ([defaul boolForKey:[NSString stringWithFormat:@"mdtask%ld",(long)indexPath.row]]) {
                [claimBtn setBackgroundImage:[UIImage imageNamed:@"Claim"] forState:UIControlStateNormal];
            }else{
                [claimBtn setBackgroundImage:[UIImage imageNamed:@"Unfished"] forState:UIControlStateNormal];
                
                [claimBtn addTarget:self action:@selector(claimBtnclicked:) forControlEvents:UIControlEventTouchUpInside];
            }
            
        
            
//        }
        
        
        
    }else if(indexPath.row<4){
        countLab.text=[NSString stringWithFormat:@"%ld/%d",(long)[defaul integerForKey:@"mainlevel"],[self.mdtaskArr[indexPath.row] intValue]];
//        if ([defaul integerForKey:@"mainlevel"]<[self.mdtaskArr[indexPath.row] intValue]) {
//            
//        }else{
            if ([defaul boolForKey:[NSString stringWithFormat:@"mdtask%ld",(long)indexPath.row]]) {
                [claimBtn setBackgroundImage:[UIImage imageNamed:@"Claim"] forState:UIControlStateNormal];
            }else{
                [claimBtn setBackgroundImage:[UIImage imageNamed:@"Unfished"] forState:UIControlStateNormal];
                
                [claimBtn addTarget:self action:@selector(claimBtnclicked:) forControlEvents:UIControlEventTouchUpInside];
            }
            
        
            
//        }
    }else{
        countLab.text=[NSString stringWithFormat:@"%ld/%d",(long)[defaul integerForKey:@"gamescore2"],[self.mdtaskArr[indexPath.row] intValue]];
//        if ([defaul integerForKey:@"gamescore2"]<[self.mdtaskArr[indexPath.row] intValue]) {
//            
//        }else{
            if ([defaul boolForKey:[NSString stringWithFormat:@"mdtask%ld",(long)indexPath.row]]) {
                [claimBtn setBackgroundImage:[UIImage imageNamed:@"Claim"] forState:UIControlStateNormal];
            }else{
                [claimBtn setBackgroundImage:[UIImage imageNamed:@"Unfished"] forState:UIControlStateNormal];
                
                [claimBtn addTarget:self action:@selector(claimBtnclicked:) forControlEvents:UIControlEventTouchUpInside];
            }
            
        
            
//        }
    }
    

    
  
    
    
    return cell;
}



-(void)claimBtnclicked:(UIButton *)senbtn{
    [senbtn removeTarget:self action:@selector(claimBtnclicked:) forControlEvents:UIControlEventTouchUpInside];
    [senbtn setBackgroundImage:[UIImage imageNamed:@"mdtaskgrayClaimed"] forState:UIControlStateNormal];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:[NSString stringWithFormat:@"mdtask%ld",(long)senbtn.tag]];
    UIView *showView=[[UIView alloc]initWithFrame:self.view.frame];
    showView.tag=102;
    showView.backgroundColor=[UIColor colorWithWhite:0.0 alpha:0.6];
    [self.view addSubview:showView];
    UIImageView *showImageView=[[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-self.view.frame.size.height*0.136*3.37/2, self.view.frame.size.height*0.25, self.view.frame.size.height*0.136*3.37, self.view.frame.size.height*0.136)];
    showImageView.image=[UIImage imageNamed:@"Youdeservethisaward"];
    [showView addSubview:showImageView];
    UIImageView *itemIView=[[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-self.view.frame.size.height*0.36/2,self.view.frame.size.height*0.32, self.view.frame.size.height*0.36, self.view.frame.size.height*0.36)];
    itemIView.image=[UIImage imageNamed:@"mddiamondimage60"];
    [showView addSubview:itemIView];
    [self.view addSubview:showView];
    UIButton *yesBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    yesBtn.frame=CGRectMake(self.view.frame.size.width/2-self.view.frame.size.height*0.06*2.89/2, self.view.frame.size.height*0.70, self.view.frame.size.height*0.06*2.89, self.view.frame.size.height*0.06);
    [yesBtn setBackgroundImage:[UIImage imageNamed:@"mdtaskyes"] forState:UIControlStateNormal];
    [yesBtn addTarget:self action:@selector(yesBtnclicked) forControlEvents:UIControlEventTouchUpInside];;
    [showView addSubview:yesBtn];
  [ [NSUserDefaults standardUserDefaults] setInteger: [[NSUserDefaults standardUserDefaults]integerForKey:@"mddiamond"]+60 forKey:@"mddiamond"];
}
-(void)yesBtnclicked{
    
    UIView *iview=(UIView*)[self.view viewWithTag:102];
    [iview removeFromSuperview];
    
    
}


@end
